package com.itheima.ai.controller;

import com.itheima.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.content.Media;
import org.springframework.util.MimeType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class chatController {

    private final ChatClient chatClient;

    private final ChatHistoryRepository chatHistoryRepository;


    /**
     * 多模态聊天改造
     */
    @RequestMapping("/chat")
    public Flux<String> chat1(@RequestParam("prompt") String prompt,
                              @RequestParam("prompt") String chatId,
                              @RequestParam(value = "files",required = false) List<MultipartFile> files){
        //保存会话ID
        chatHistoryRepository.save("chat",chatId);
        if (files == null || files.isEmpty()) {
            //无附件，纯文本聊天
            return textChat(prompt, chatId);
        } else {
            return multiModalChat(prompt, chatId, files);
        }
    }

    private Flux<String> multiModalChat(String prompt, String chatId, List<MultipartFile> files) {
        //处理文件
        List<Media> medias = files.stream().map(file -> new Media(
                        MimeType.valueOf(Objects.requireNonNull(file.getContentType())),
                        file.getResource()
                )
        ).toList();
        return chatClient.prompt()
                .user(p -> p.text(prompt).media(medias.toArray(Media[]::new)))
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream()
                .content();
    }

    private Flux<String> textChat(String prompt, String chatId) {
        return chatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
    }


    //    @RequestMapping(value = "/chat",produces = "text/html;charset=UTF-8")
    //    public Flux<String> chat1(String prompt,String chatId) throws Exception{
    //        //保存会话ID
    //        chatHistoryRepository.save("chat",chatId);
    //        return chatClient.prompt()
    //                .user(prompt)
    //                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId))
    //                .stream()
    //                .content();
    //    }





//    @RequestMapping("/chat2")
//    public String chat2(String prompt) throws Exception{
//        return chatClient.prompt()
//                .user(prompt)
//                .call()
//                .content();
//    }
}
